1 import java.net.*;
2 import java.io.*;
3 import java.util.*;
4
5 /**
6
7 WARNING! I modified this. -Erich
8
9 CGIStuff is a class that holds methods useful to anyone writing a CGI back-end in Java.
10 It's supplied as part of the JavaCGI package.
11 @author Ben Last (ben@hypereality.co.uk)
12 @version 1.0, 4/7/96
13 */
14
15 public class CGIpost
16 {
17 //full of class methods, really.
18
19 /**
20 Split a QUERY_STRING (or the contents of stdin for a POST) into a HashTable.
21 The method doesn't expect <VAR>queryString</VAR> to have been urlDecoded. It will
22 decode all the variables and names.
23 */
24 public static Hashtable splitVars(String queryString)
25 {
26 return splitCodedVars(queryString, true);
27 }
28
29 protected static Hashtable splitCodedVars(String queryString, boolean decode)
30 {
31 Hashtable vars = new Hashtable();
32 String name = null;
33 boolean asName = true;
34
35 //We mustn't decode the string yet because that might introduce more '&' characters
36 //which we don't want.
37
38 //split at every '&' or '=' into substrings. We need also to handle the
39 //fact that the final variable doesn't end in an '&'.
40 StringTokenizer st = new StringTokenizer(queryString, "&=", true);
41 while(st.hasMoreTokens())
42 {
43 String var = st.nextToken();
44 //if we've been returned an '=' or an '&', that tells us
45 //what the next token should be treated as.
46 if(var.equals("="))
47 {
48 asName = false; //next token is a value
49 continue;
50 }
51
52 if(var.equals("&"))
53 {
54 //we may have a name, but no value, in which case we add it as an
55 //empty property.
56 if(name != null)
57 {
58 vars.put(name,"");
59 name = null;
60 }
61 asName = true; //next token is a name
62 continue;
63 }
64
65 if(asName)
66 {
67 name = decode ? URLDecoder.decode(var) : var;
68 continue;
69 }
70
71 //we have a value. If we also have a name, then add as a property.
72 if(name != null)
73 {
74 vars.put(name, decode ? URLDecoder.decode(var) : var);
75 name = null;
76 }
77 }
78
79 //if name is not null here, we had a trailing empty valueless variable.
80 if(name != null)
81 vars.put(name,"");
82
83 return vars;
84 }
85
86 public static String joinHashtableVars(Hashtable vars) {
87 boolean first = true;
88 String key, value;
89 String ret = "";
90
91 for (Enumeration keys = vars.keys(); keys.hasMoreElements(); first = false) {
92 key = (String) keys.nextElement();
93
94 if (!first) {
95 ret += "&";
96 }
97 ret += URLEncoder.encode(key) + "=" + URLEncoder.encode((String) vars.get(key));
98 }
99
100 return ret;
101 }
102
103 public static String post(URL url, Hashtable headers, Hashtable data) throws IOException {
104 String content, line, response, key;
105 HttpURLConnection connection;
106 DataInputStream in;
107 DataOutputStream out;
108 int responseCode;
109 byte[] dat;
110
111 // Create the URL and its connection
112 connection = (HttpURLConnection) url.openConnection();
113
114 // Setup our connection state
115 connection.setRequestMethod("POST");
116 connection.setDoInput(true);
117 connection.setDoOutput(true);
118 connection.setUseCaches(false);
119
120 // Build the content string from the hashtable
121 content = CGI.joinHashtableVars(data);
122
123 // We want to do a post operation
124 connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
125 connection.setRequestProperty("Content-length", new Integer(content.length()).toString());
126
127 for (Enumeration keys = headers.keys(); keys.hasMoreElements(); ) {
128 key = (String) keys.nextElement();
129 connection.setRequestProperty(key, (String) headers.get(key));
130 }
131
132 // Get an input and output stream for sending/receiving data
133 out = new DataOutputStream(connection.getOutputStream());
134
135 // convert to bytes
136 int len = content.length();
137 dat = content.getBytes();
138
139 // System.out.println("content = " + content);
140 System.out.println("len = " + len);
141 // This is just fucking bizaar. If you comment out the line above,
142 // the friggen thing stops working...
143
144 // Send the data and close the output stream
145 out.write(dat, 0 , len);
146 out.flush();
147 out.close();
148
149 // Read the server response
150 response = "";
151 responseCode = connection.getResponseCode();
152 if (responseCode != 200) {
153 IOException e = new IOException("Server Error: " + responseCode + " " + connection.getResponseMessage());
154 throw(e);
155 }
156 in = new DataInputStream(connection.getInputStream());
157 while ((line = in.readLine()) != null) {
158 response += line + "\n";
159 }
160
161 in.close();
162
163 return response;
164 }
165
166 }
|